/*
 * Created by JFormDesigner on Fri Apr 15 22:59:06 CST 2022
 */

package com.ZengQi.event.view;

import com.ZengQi.event.controller.AdminCommentController;
import com.ZengQi.event.entity.Comment;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author QI Zeng
 */
public class AdminCommentView extends JFrame {
    private List<Comment>list=null;
    private AdminCommentController adminCommentController=new AdminCommentController();
    private Comment thisComment=null;

    public AdminCommentView() {
        initComponents();
        list=adminCommentController.allComment();
        if(list!=null){
            int i=0;
            DefaultTableModel dtm=(DefaultTableModel) table1.getModel();
            dtm.setRowCount(0);
            for(Comment comment:list){
                Vector<String> vector=new Vector<String>();
                vector.add(comment.getCommentPublisher());
                vector.add(comment.getCommentContent());
                vector.add(comment.getTopicName());
                dtm.addRow(vector);
            }

        }

        setVisible(true);
        setLocationRelativeTo(null);
    }

    private void deleteBtn(ActionEvent e) {
        if(thisComment==null){
            JOptionPane.showMessageDialog(null,"请先选择要删除的评论");

        }else{
            boolean b = adminCommentController.deleteComment(thisComment);
            if(b){
                JOptionPane.showMessageDialog(null,"删除成功");
                new AdminCommentView();
                dispose();
            }else {
                JOptionPane.showMessageDialog(null,"删除失败，请联系管理员");

            }

        }
    }

    private void table1MousePressed(MouseEvent e) {
        mousePressed(e);
    }

    /**
     * 表格的事件监听
     * 思路是选中某一行则显示某一行的具体数据
     * @param e
     */
    private void mousePressed(MouseEvent e) {
        int row=table1.getSelectedRow();
        Comment comment=list.get(row);
        thisComment=list.get(row);
        commentTxt.setText(comment.getCommentPublisher());
        contentArea1.setText(comment.getCommentContent());
        topicTxt.setText(comment.getTopicName());
    }

    /**
     * 组件布局及初始化
     */
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        panel1 = new JPanel();
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        commentTxt = new JTextField();
        topicTxt = new JTextField();
        scrollPane2 = new JScrollPane();
        contentArea1 = new JTextArea();
        deleteBtn = new JButton();

        //======== this ========
        setTitle("\u505a\u9898\u5e2e");
        setIconImage(new ImageIcon(getClass().getResource("/bang.png")).getImage());
        Container contentPane = getContentPane();

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null},
                    {null, null, null},
                },
                new String[] {
                    "\u8bc4\u8bba\u8005", "\u8bc4\u8bba\u5185\u5bb9", "\u8bc4\u8bba\u9898\u76ee"
                }
            ) {
                boolean[] columnEditable = new boolean[] {
                    false, false, false
                };
                @Override
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return columnEditable[columnIndex];
                }
            });
            {
                TableColumnModel cm = table1.getColumnModel();
                cm.getColumn(0).setResizable(false);
                cm.getColumn(0).setPreferredWidth(175);
                cm.getColumn(1).setResizable(false);
                cm.getColumn(1).setPreferredWidth(340);
                cm.getColumn(2).setResizable(false);
                cm.getColumn(2).setPreferredWidth(165);
            }
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    table1MousePressed(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //======== panel1 ========
        {

            //---- label1 ----
            label1.setText("\u8bc4\u8bba\u8005");

            //---- label2 ----
            label2.setText("\u8bc4\u8bba\u9898\u76ee");

            //---- label3 ----
            label3.setText("\u8bc4\u8bba\u5185\u5bb9");

            //---- commentTxt ----
            commentTxt.setEditable(false);

            //---- topicTxt ----
            topicTxt.setEditable(false);

            //======== scrollPane2 ========
            {

                //---- contentArea1 ----
                contentArea1.setEditable(false);
                scrollPane2.setViewportView(contentArea1);
            }

            //---- deleteBtn ----
            deleteBtn.setText("\u5220\u9664\u8bc4\u8bba");
            deleteBtn.addActionListener(e -> deleteBtn(e));

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label2, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addComponent(commentTxt, GroupLayout.PREFERRED_SIZE, 204, GroupLayout.PREFERRED_SIZE)
                            .addComponent(topicTxt, GroupLayout.PREFERRED_SIZE, 184, GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(440, Short.MAX_VALUE))
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(scrollPane2, GroupLayout.PREFERRED_SIZE, 226, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 147, Short.MAX_VALUE)
                        .addComponent(deleteBtn, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE)
                        .addGap(97, 97, 97))
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label1)
                            .addComponent(commentTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label2)
                            .addComponent(topicTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(65, 65, 65)
                                .addComponent(label3))
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(39, 39, 39)
                                .addComponent(scrollPane2, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(31, Short.MAX_VALUE))
                    .addGroup(GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
                        .addContainerGap(151, Short.MAX_VALUE)
                        .addComponent(deleteBtn)
                        .addGap(132, 132, 132))
            );
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(6, 6, 6)
                            .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 812, Short.MAX_VALUE)))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 164, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JScrollPane scrollPane1;
    private JTable table1;
    private JPanel panel1;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField commentTxt;
    private JTextField topicTxt;
    private JScrollPane scrollPane2;
    private JTextArea contentArea1;
    private JButton deleteBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
